package com.mall.ibatis.persistence.sqlmapdao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mall.ibatis.bean.Frame;
import com.mall.ibatis.bean.FunctionMethod;
import com.mall.ibatis.bean.Operator;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.ibatis.persistence.iface.OperatorDAO;

import com.ibatis.dao.client.DaoManager;

public class OperatorSqlMapDAO extends BaseSqlMapDao implements OperatorDAO
{
	public OperatorSqlMapDAO(DaoManager daoManager)
	{
		super(daoManager);
	}
	
	@SuppressWarnings("unchecked")
	public List<Operator> selectAllOperator() throws Exception
	{
		return (List<Operator>)queryForList("select_operator_list",null);
	}
	
	@SuppressWarnings("unchecked")
	public List<Operator> selectAllOperator(Operator operator) throws Exception
	{
		return (List<Operator>)queryForList("select_operator_list",operator);
	}
	
	public Integer selectAllOperatorCount(Operator operator) throws Exception
	{
		return (Integer)queryForObject("select_operator_list_count",operator);
	}
	
	public Operator selectOperator(Operator operator)  throws Exception
	{
		return (Operator)queryForObject("select_operator", operator);
	}
	
	public String insertOperator(Operator operator) throws Exception
	{
		return (String)insert("insert_operator",  operator);
	}
	public void updateOperator(Operator operator) throws Exception
	{
		update("update_operator",  operator);
	}
	
	public void deleteOperator(Operator operator) throws Exception
	{
		update("delete_operator",  operator);
	}

	public ReturnMessage validOperator(String username,String loginIP,String password) throws Exception
	{
		// TODO Auto-generated method stub
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		parameters.put("p_username", username);
		parameters.put("p_loginip", loginIP);
		parameters.put("p_password", password);
		parameters.put("p_flag", null);
		parameters.put("p_message", null);
		
		queryForObject("ps_login", parameters);
		
		ReturnMessage returnMessage = new ReturnMessage(((Integer)parameters.get("p_flag")).toString(),(String)parameters.get("p_message"));
		
		return returnMessage;
	}

	public ReturnMessage modifyPassword(String operatorID, String oldPassword,
			String newPassword, String confirmPassword) throws Exception
	{
		// TODO Auto-generated method stub
		//p_operator_ID varchar2,p_old_password varchar2,p_new_password varchar2,p_confirm_password varchar2,p_flag out number , p_message out varchar2
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		parameters.put("p_operator_ID", operatorID);
		parameters.put("p_old_password", oldPassword);
		parameters.put("p_new_password", newPassword);
		parameters.put("p_confirm_password", confirmPassword);
		parameters.put("p_flag", null);
		parameters.put("p_message", null);
		
		queryForObject("ps_modify_password", parameters);
		
		ReturnMessage returnMessage = new ReturnMessage(((Integer)parameters.get("p_flag")).toString(),(String)parameters.get("p_message"));
		return returnMessage;
	}

	public void updateOperatorWithoutPassword(Operator operator)
			throws Exception
	{
		// TODO Auto-generated method stub
		update("update_operator_without_password",operator);
	}
	
	public void updateOperatorPassword(Operator operator)
		throws Exception
	{
		update("update_operator_password", operator);
	}

	public void getOperatorRoles(String operatorID, String roleIDs,
			String roleNames) throws Exception
	{
		// TODO Auto-generated method stub
		Map<String,Object> parameters = new HashMap<String, Object>();
		
		parameters.put("p_operator_id", operatorID);
		parameters.put("p_roles", roleIDs);
		parameters.put("p_roles_name", roleNames);
		queryForObject("ps_get_roles", parameters);
	}
	
	//ryh	
	//保存用户和数据范围之前先删除先前的对应关系来完成更新操作
	public void deleteDataScopeByOpId(String opId) throws Exception{
		delete("delete_data_scope_by_id",opId);
	}
	//保存用户和数据范围的对应关系
	public void addDataScope(String opId, String str) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("opId",opId);
		map.put("str",str);
		insert("add_data_scope",map);
	}
	
	//向服务器请求勾选了哪些方法并返回给修改树状窗口
	@SuppressWarnings("unchecked")
	public List<FunctionMethod> methodIsCheck(String id) {
		return queryForList("operator_methods_is_checked",id);
	}
	
	//在添加方法和功能的对应关系前。先按角色的id来删除关系，实现覆盖操作
	public int deleteMethodFunction(String id) throws Exception{
		return delete("delete_operator_method_function",id);
	}
	
	//添加方法和功能的对应关系
	//FunctionMethod中的role_id用来装operator_id
	public String addMethodFunction(FunctionMethod fm) {
		return (String)insert("add_operator_method_function",fm);
	}

	//通过用户的Id查询出相应的数据范围名称以及Id
	@SuppressWarnings("unchecked")
	public List<String> selectDataScope(String opId) {
		return queryForList("select_data_scope",opId);
	}

	//管理架构功能树一次性加载
	@SuppressWarnings("unchecked")
	public List<Frame> frame() {
		return queryForList("frame_all_tree",null);
	}

	//向服务器请求勾选了哪些功能方法并返回给修改树状窗口
	@SuppressWarnings("unchecked")
	public List<String> functionIsCheck(String id) {
		return queryForList("operator_function_is_check",id);
	}

	//先删除已经有的功能
	public void deleteOperatorFunction(String id) throws Exception{
		delete("delete_operator_function",id);
		
	}

	//批量插入功能集合
	public void addOperatorFunction(String id, String functionId) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("operatorId",id);
		map.put("functionId",functionId);
		insert("add_operator_function",map);
		
	}
	//ryh
	
	/**
	 * 得到街道收费人员列表
	 * @param street 街道编号
	 * @return 收费人员列表
	 * */
	@SuppressWarnings("unchecked")
	public List<Operator> selectStreetCharger(String street) throws Exception
	{
		return (List<Operator>)queryForList("select_street_charger",street);
	}
}
